set printback=OFF.

TITLE LIS Cross-section Data center in Luxembourg.

TITLE email: usersupport@lisdatacenter.org .

TITLE LIS Self Teaching Package 2022.

TITLE Part I: Inequality, poverty, and social policy.
TITLE SPSS version.

TITLE last change of this version of the syntax: 15-01-2022.


TITLE Exercise 4: Inequality: The Gini Index.

define ginicalc () 
sort cases by inc_var (a).  
compute cumwgt = cumwgt + wt_var.  
leave cumwgt.  
aggregate outfile= * 
   mode = addvariables 
    /break= did 
    /meany= mean(inc_var) 
    /meanr= mean (cumwgt) 
    /n=n. 
compute devy= inc_var - meany.  
compute rank= cumwgt/n.  
compute devr = (rank - 0.5).  
compute prod= devy*devr.  
aggregate outfile= * 
    /break=did 
    /sumprod= sum(prod) 
    /meany= mean(inc_var) 
    /n=n. 
compute cov= sumprod/(n-1). 
compute gini=cov*2/meany. 
formats gini (f10.4). 
descriptives var=gini .  
!enddefine . 
 
define dataprepare ().  
SET ERRORS OFF. 
compute miss_comp = 0.
if (missing(dhi) or missing(hifactor) or missing(hpub_i) or missing(hpub_u) or missing(hpub_a) or missing(hiprivate) or missing(hxitsc)) miss_comp = 1.
select if miss_comp eq 0.
* select only records if dhi filled. 
select if not missing(dhi) . 
* create top and bottom coded household disposable income.
compute dhi_tb = dhi.
* recode negaive dhi into zero
if (dhi_tb<0) dhi_tb=0.
EXECUTE. 
compute dhi_log = ln(dhi_tb). 
EXECUTE. 
if (missing(dhi_log) & NOT(missing(dhi_tb))) dhi_log=0. 
EXECUTE. 
* create person weight as hwgt times number of household members. 
compute wt = hpopwgt*nhhmem . 
* create child weight as hwgt times number of household members under 18. 
weight by wt. 
!enddefine . 

define decilecalc (). 
preserve .  
set tvars names tnumbers values.  
dataset declare decileratio.  
WEIGHT BY hpopwgt. 
sort cases by did.  
split file by did.  
OMS      
 / select tables      
 / if command = ['Frequencies'] subtypes=['Statistics']      
 /destination format = sav        outfile = 'decileratio'      
 /columns sequence = [l1 r2] .  
frequencies variables = dhi_log 
  /percentiles = 25 50 75 
  /format = notable . 
OMSEND. 
weights off. 
restore. 
match files file = * 
  /table = 'decileratio' 
  /rename (var1 = did) 
  /by did . 
!enddefine .  
 
define topbottom (). 
weight by wt. 
COMPUTE iqr=dhi_log_75-dhi_log_25. 
EXECUTE. 
* detect upper bound for extreme values 
COMPUTE upper_bound=dhi_log_75 + (iqr * 3). 
EXECUTE. 
COMPUTE lower_bound=dhi_log_25 - (iqr * 3). 
EXECUTE. 
* top code income at upper bound for extreme values 
if dhi_tb>exp(upper_bound) dhi_tb=exp(upper_bound).  
EXECUTE. 
* bottom code income at lower bound for extreme values 
if dhi_tb<exp(lower_bound) dhi_tb=exp(lower_bound). 
EXECUTE. 
* create equivalised income, set equivalence scale as square root of household members 
compute edhi_tb = dhi_tb/(nhhmem**0.5).
compute pcdhi_tb = dhi_tb/nhhmem.
!enddefine .
 
get file = gt06h /keep=did dhi hifactor hpub_i hpub_u hpub_a hiprivate hxitsc hpopwgt nhhmem grossnet. 

* run the dataprepare, decilecalc and topbottom
dataprepare.
decilecalc.
topbottom.

* set two input parameters (weighting and income variable). 
compute wt_var =  hpopwgt. 
compute inc_var = dhi_tb. 

* weight the dataset with the weight variable complying with the inequality measure
weight by hpopwgt . 

* run the gini calculation block
ginicalc.  
 
get file = gt06h /keep=did dhi hifactor hpub_i hpub_u hpub_a hiprivate hxitsc hpopwgt nhhmem grossnet. 

* set two input parameters (weighting and income variable). 
dataprepare.
decilecalc.
topbottom.
  
compute wt_var = hpopwgt*nhhmem. 
compute inc_var = pcdhi_tb. 
weight by wt_var. 
ginicalc.  
  
get file = gt06h /keep=did dhi hifactor hpub_i hpub_u hpub_a hiprivate hxitsc hpopwgt nhhmem grossnet. 

* set two input parameters (weighting and income variable). 
dataprepare.
decilecalc.
topbottom.
  
compute wt_var = hpopwgt*nhhmem. 
compute inc_var = edhi_tb. 
weight by wt_var. 
ginicalc. 
